<template>
	<el-dialog draggable overflow :title="'回潮检测'" v-model="visible" :width="1000" destroy-on-close
		@closed="$emit('closed')">
		<sk-tabs type="border-card">
			<el-tab-pane ref="tab" label="基础信息">
				<el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px" label-position="right">
					<el-row>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
							<el-row>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="往来" prop="code">
										<el-input v-model="form.company.name" readonly class="readonly"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="产品" prop="code">
										<el-input v-model="form.material.name" readonly class="readonly"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="制单人" prop="code">
										<el-input v-model="form.creator.name" readonly class="readonly"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="入库人" prop="code">
										<el-input label="入库人" v-model="form.code" readonly class="readonly"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
							<el-row>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="码单" prop="code">
										<div>
											<el-input v-model="form.auxiliary_quantity
		" readonly class="readonly">
												<template #prepend>
													<span class="red">包</span></template></el-input>
										</div>

										<div>
											<el-input v-model="form.quantity" readonly class="readonly">

												<template #prepend>
													<span class="red">重</span></template></el-input>
										</div>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="实际" prop="code">
										<div>
											<el-input v-model="form.fect_auxiliary_quantity
		">

												<template #prepend>
													<span class="red">包</span></template></el-input>
										</div>
										<div>
											<el-input v-model="form.fect_quantity" readonly class="readonly">

												<template #prepend>
													<span class="red">重</span></template></el-input>
										</div>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="约定信息" prop="code">
										<div>
											<el-input v-model="form.promise_weight" readonly class="readonly">

												<template #prepend>
													<span class="red">标重</span></template></el-input>
										</div>
										<div>
											<el-input v-model="form.promise_resurgence" readonly class="readonly">

												<template #prepend>
													<span class="red">回潮</span></template></el-input>
										</div>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="其他信息" prop="code">
										<div>
											<el-input v-model="form.type.name" readonly class="readonly">

												<template #prepend>
													<span class="red">形态</span></template></el-input>
										</div>
										<div>
											<el-input v-model="form.lot_name" readonly class="readonly">

												<template #prepend>
													<span class="red">批号</span></template></el-input>
										</div>
									</el-form-item>
								</el-col>
							</el-row>
							<el-row class="inputdata" justify="space-around" :pull="2">
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="库位" prop="location_id">
										<skAutoChoose v-model="location.name" :name="'location'" :assign="form">
										</skAutoChoose>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="装卸单" prop="carry.code">
										<skAutoChoose v-model="form.carry.code" :name="'carry'" :assign="form"
											@select="selectCarry">
										</skAutoChoose>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="搬运工" prop="carry.company">
										<skAutoChoose v-model="form.carry.company.name" :name="'company'"
											:assign="form.carry" :params="[{ category_id: 54 }]">
										</skAutoChoose>
									</el-form-item>
								</el-col>
								<el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
									<el-form-item label="搬运费" prop="carry.amount">
										<el-input v-model="form.carry.amount" clearable></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</el-col>
						<el-col :xs="24" :sm="24" :md="10" :lg="10" :xl="10">
							<div>
								<sc-VxeTable ref="xGrid1" :gridEvts="gridEvents" :gridOps="gridOps" :data="form.details"
									:tablecode="'storageActionPurchaseCheckhc'">
								</sc-VxeTable>
							</div>
						</el-col>
						<el-col :xs="24" :sm="24" :md="14" :lg="14" :xl="14">
							<el-row justify="center">
								<el-col :span="12">
									<el-form-item label="每包个数" prop="code">
										<el-input label="每包个数" @input="computHank" v-model="form.perhank"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="总个数" prop="code">
										<el-input label="总个数" v-model="form.hank" readonly class="readonly"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="毛重(Kg/包)" prop="code">
										<el-input v-model="form.gross" readonly class="readonly"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="皮重(Kg/包)" prop="code">
										<el-input v-model="form.tare" placeholder="可手动更改" class="input-with-select">

											<template #prepend>
												<skDicSelect v-model="form.tare" :valueEqKey="false" :code="'tare'">
												</skDicSelect>
											</template>

											<template #append>
												<span class="red">皮重</span>
											</template>
										</el-input>
									</el-form-item> </el-col><el-col :span="12">
									<el-form-item label="实际回潮" prop="measured_resurgence">
										<el-input v-model="form.measured_resurgence">

											<template #append><span class="red">%</span></template></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="标准回潮" prop="standard_resurgence">
										<el-input v-model="form.standard_resurgence" readonly class="readonly">

											<template #append><span class="red">%</span></template></el-input>
									</el-form-item> </el-col><el-col :span="12">
									<el-form-item label="净重(Kg/包)" prop="suttle_weight">
										<el-input v-model="form.suttle_weight" readonly class="readonly">

											<template #append>
												<el-input v-model="form.odd" class="readwrite"
													placeholder="填写零头"></el-input></template></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="标重(Kg/包)" prop="indicated_weight">
										<el-input v-model="form.indicated_weight" readonly class="readonly"></el-input>
									</el-form-item> </el-col><el-col :span="12">
									<el-form-item label="每吨包数" prop="packets_number">
										<el-input v-model="form.packets_number"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="总标重" prop="total_indicated_weight">
										<el-input v-model="form.total_indicated_weight
		" readonly class="readonly"></el-input>
									</el-form-item> </el-col><el-col :span="12">
									<el-form-item label="打包绞数" prop="prohank">
										<el-input v-model="form.prohank"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="检测结果" prop="result_id">
										<el-radio-group v-model="form.result_id">
											<el-radio :label="1">合格</el-radio>
											<el-radio :label="-1">不合格</el-radio>
										</el-radio-group>
									</el-form-item> </el-col><el-col :span="24">
									<el-form-item label="备注" prop="remark">
										<el-input label="搬运工" v-model="form.remark"></el-input>
									</el-form-item>
								</el-col> </el-row></el-col>
					</el-row>
				</el-form>
			</el-tab-pane>
			<el-tab-pane ref="tab1" label="附件">
				<sk-upload v-model="form.attachments" :limit="10" draggable>
				</sk-upload>
			</el-tab-pane>
		</sk-tabs>

		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">入库</el-button>
		</template>
	</el-dialog>
</template>

<script>
import XEUtils from "xe-utils";
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import { ElMessageBox } from "element-plus";
export default {
	emits: ["success", "closed"],
	components: { skDicSelect, skAutoChoose },
	data() {
		return {
			openChoose: { order: false },
			gridEvents: {
				removeRow: async ({ button }) => {
					const type = await ElMessageBox.confirm(
						"您确定要删除该数据?"
					);

					if (type === "confirm") {
						let count = 0;
						let defaultData = this.form.details;
						defaultData.filter((item, index) => {
							button.selectRecords.some((v) => v == item) &&
								this.form.details.splice(index - count++, 1);
						});
						this.computAva(defaultData);
						return;
					}
				},
			},
			gridOps: {
				height: 350,
				proxyConfig: null,
				editConfig: { trigger: "click" },
				columns: [
					{
						field: "gross",
						event: {
							keyup: (param) => {
								this.computAva(param.params.data);
							},
						},
					},
				],
			},

			form: {
				id: "",
				code: "",
				company: { name: "" },
				carry: { company: {} },
				attachments: [],
				details: [],
			},
			location: {
				name: "",
			},
			rules: {
				location_id: [{ required: true, message: "库位必选！" }],
			},
			visible: false,
			isSaveing: false,
		};
	},
	updated() { },
	watch: {
		"form.tare": {
			handler() {
				this.computSuttleWeight();
			},
			immediate: true,
		},
		"form.gross": {
			handler() {
				this.computSuttleWeight(); //计算净重
			},
			immediate: true,
		},
		"form.measured_resurgence": {
			handler() {
				this.computIndicatedWeight(); //计算包回潮
			},
			immediate: true,
		},
		"form.suttle_weight": {
			handler() {
				this.computIndicatedWeight(); //计算包回潮
				this.computQuantity(); //计算总重
			},
			immediate: true,
		},
		"form.indicated_weight": {
			handler() {
				this.computTotalIndicatedWeight(); //计算吨标重
			},
			immediate: true,
		},
		"form.packets_number": {
			handler() {
				this.computTotalIndicatedWeight(); //计算吨标重
			},
			immediate: true,
		},
		"form.fect_auxiliary_quantity": {
			handler() {
				this.computQuantity(); //计算总重
				this.computHank();
			},
			immediate: true,
		},
		"form.total_indicated_weight": {
			handler() {
				this.checkResult(); //检验结果
			},
			immediate: true,
		},
	},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;

			return this;
		},
		//检验结果
		checkResult() {
			if (
				XEUtils.toNumber(this.form.total_indicated_weight) >=
				XEUtils.toNumber(this.form.standard_weight)
			) {
				this.form.result_id = 1;
			} else {
				this.form.result_id = -1;
			}
		},
		//计算总重
		computQuantity() {
			this.form.fect_quantity = XEUtils.toFixed(
				XEUtils.toNumber(this.form.suttle_weight) *
				XEUtils.toNumber(this.form.fect_auxiliary_quantity),
				2
			);
		},
		//计算平均重
		computAva(param) {
			let sum = XEUtils.sum(param, "gross");
			let count = param.length;
			if (count > 0) {
				this.form.gross = XEUtils.toFixed(sum / count, 2);
			}
		},
		//计算绞数
		computHank() {
			this.form.hank = XEUtils.toInteger(
				XEUtils.toNumber(this.form.perhank) *
				XEUtils.toNumber(this.form.fect_auxiliary_quantity)
			);
		},
		//计算净重
		computSuttleWeight() {
			this.form.suttle_weight = XEUtils.toFixed(
				XEUtils.toNumber(this.form.gross) -
				XEUtils.toNumber(this.form.tare),
				2
			);
		},
		//计算标重包
		computIndicatedWeight() {
			this.form.indicated_weight = XEUtils.toFixed(
				XEUtils.toNumber(this.form.suttle_weight) * this.computRat(),
				2
			);
		},
		//计算吨标重
		computTotalIndicatedWeight() {
			this.form.total_indicated_weight = XEUtils.toFixed(
				XEUtils.toNumber(this.form.indicated_weight) *
				XEUtils.toNumber(this.form.packets_number),
				2
			);
		},
		//计算缩率
		computRat() {
			return (
				(1 + XEUtils.toNumber(this.form.standard_resurgence) / 100) /
				(1 + XEUtils.toNumber(this.form.measured_resurgence) / 100)
			);
		},
		selectCarry(data) {
			if (typeof data.code == "undefined") {
				data = { company: {} };
			}
			this.form.carry = data;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.storage.moisture.save.post(
						this.form
					);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("success", this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功");
					}
				}
			});
		},
		async getItems(id) {
			this.isSaveing = true;
			var res = await this.$API.storage.moisture.info.get({
				filter: [{ purchase_storage_id: id }],
			});

			if (res.code == 200) {
				Object.assign(this.form, res.data);
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//表单注入数据
		setData(data) {

			Object.assign(
				this.form,
				this.$TOOL.objCopy(this.$TOOL.removeNull(data))
			);
			this.form.fect_quantity = 0;
			this.form.details = [];
			this.form["purchase_storage_id"] = data["id"];
			delete this.form["id"];
			if (this.form.material.standard_resurgence != null)
				this.form.standard_resurgence =
					this.form.material.standard_resurgence;
			//不带出库位
			delete this.form.location_id

			this.getItems(data["id"]);
			this.isSaveing = false;
		},
	},
};
</script>

<style>
.el-input-group__prepend,
.el-input-group__append {
	padding: 0;
	background-color: var(--el-fill-color-blank);
}

.el-input-group__prepend .el-select {
	margin: 0px;
	border: 0px;
}

.readonly div {
	background-color: #f0efef;
}

.readwrite div {
	background-color: #ffffff;
}

.red {
	color: #f56c6c;
	min-width: 24px;
	text-align: center;
}

.inputdata {
	background-color: #fbebeb;
	padding-bottom: 10px;
}

.inputdata .el-form-item--small {
	margin-bottom: 5px;
}
</style>
